/**
 * routes是一个数组，数组中的每一个对象是一个路由对象，路由中常用的属性
 * path:路径:以/作为开头的
 * component:组件
 */
// import DiscoveryMusic from '@/views/DiscoveryMusic.vue'
// import MyMusic from '@/views/MyMusic.vue'
// import FriendMusic from '@/views/FriendMusic.vue'
// import Tuijian from '@/views/Tuijian.vue'
import Paihangbang from '@/views/Paihangbang.vue'
import GeDan from '@/views/GeDan.vue'
import ZhuBoDianTai from '@/views/ZhuBoDianTai.vue'
import GeShou from '@/views/GeShou.vue'
import XinDieShangJia from '@/views/XinDieShangJia.vue'
import CategoryArt from '@/views/CategoryArt.vue'
import TopListCategory from '@/views/TopListCategory.vue'
import NotFound from '@/views/NotFound.vue'
import Vue from 'vue'
const routes = [
    {
        path: '*',
        component: () => import('@/views/NotFound.vue')
    },
    {
        path: '',
        component: () => import('@/views/DiscoveryMusic.vue'),
        children: [
            {
                path: 'recomm',//嵌套路由如果不以/开头，它的path就是路由的path+自己路由的path
                component: () => import('@/views/Tuijian.vue'),
                meta: {
                    isKeepAlive: true
                }
            },
            {
                path: 'toplist',
                component: Paihangbang,
                children: [
                    {
                        name: 'topListName',
                        path: '',//默认路由的配置
                        component: TopListCategory,
                        props(route) {
                            return {
                                id: route.query.id
                            }
                        }
                    }
                ]

            },
            {
                path: 'playlist',
                component: GeDan
            },
            {
                path: 'djradio',
                component: ZhuBoDianTai,
                meta: {
                    isKeepAlive: true,
                    requiresAuth: true
                }
            },
            {
                path: 'artist',
                component: GeShou,
                children: [
                    {
                        name: 'CategoryArt',
                        path: 'cat/:cid',
                        component: CategoryArt,
                        props: true
                    }
                ]
            },
            {
                path: 'album',
                component: XinDieShangJia,
                meta: {
                    requiresAuth: true
                }
            }
        ],
        async beforeEnter(to, from, next) {
            //获取到本地token信息
            const isToken = localStorage.getItem('token')
            //需要鉴权
            if (isToken) {
                try {
                    await Vue.prototype.$api.users.getUserInfoApi()
                    next()
                } catch (error) {
                    Vue.prototype.$message.warning('toke已经过期，请重新登录')
                    next('/login')
                }
            } else {
                Vue.prototype.$message.warning("您还没有登录请登录")
                next("/login")
            }
       }
    },
    {
        path: '/my',
        redirect: '/me'//重定向

    },
    {
        path: '/me',
        component: () => import('@/views/MyMusic.vue')

    },
    {
        path: '/friend',
        component: () => import('@/views/FriendMusic.vue')
    },
    {
        path: '/mine',
        component: () => import('@/views/Mine.vue')
    },
    {
        path: '/login',
        component: () => import('@/views/Login.vue')
    }
]
export default routes